// pages/user/login/login.js
//获取应用实例
// 登录
var graceChecker = require('../../../utils/graceChecker.js');
import formRuleConfig from '../../../utils/formRule.config.js';
import api from '../../../api/index.js';
var inputContent = {}; //输入内容
Page({
  /**
   * 页面的初始数据
   */
  data: {
    openId: '',
    paracont: "获取验证码", //验证码文字
    vcdisabled: true, //验证码按钮状态
    verifycode: "", //返回的验证码
    phoneNum: '', //手机号码
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      openId: options.openId
    })
  },
  //绑定
  loginSubmit(e) {
    let parmas = e.detail.value;
    console.log('11111111', parmas);
    let that = this;
    let obj = {};
    //验证码登录
    if (!that.verifyFormat(parmas)) return;
    //通过AES加密参数
    obj = {
      username: parmas.username,
      code:parmas.code,
      userId:this.data.openId
    }
    //去绑定
    api.bindingWeChat(obj).then(res => {
      console.log(res)
      wx.setStorageSync('token', res.data.data.DTG_MARK);
      wx.setStorageSync('logInfo', JSON.stringify(res.data.data.info));
      // 去首页
      wx.switchTab({
        url: '/pages/index/index',
      })
    }).catch(() => {})
  },

  //验证格式
  verifyFormat(parmas) {
    //验证格式
    let checkRes = {};
    //登录方式  0 验证码登录  1 账号密码登录
    checkRes = graceChecker.check(parmas, formRuleConfig.loginCodeRule);
    if (!checkRes) {
      wx.showToast({
        title: graceChecker.error,
        icon: "none"
      });
      return false;
    }
    return true;
  },

  //获取用户输入 手机格式
  bindChange(e) {
    // console.log(e);
    let that = this;
    inputContent = e.detail.value;
    if ((/^1[3456789]\d{9}$/.test(inputContent))) {
      that.setData({
        vcdisabled: false,
      })
    } else if (!(/^1[3456789]\d{9}$/.test(inputContent))) {
      that.setData({
        vcdisabled: true
      })
    }
  },
  //获取验证码
  getVerifyCode() {
    let that = this;
    var second = 60,
      timePromise = undefined;
    timePromise = setInterval(function () {
      if (second <= 0 && (/^1[3456789]\d{9}$/.test(inputContent))) {
        clearInterval(timePromise);
        that.setData({
          paracont: "获取验证码",
          vcdisabled: false
        })
      } else {
        if (second <= 0) {
          clearInterval(timePromise);
          that.setData({
            paracont: "获取验证码",
            vcdisabled: false
          })
        } else {
          that.setData({
            paracont: second + "秒后重试",
            vcdisabled: true
          })
        }

        second--;
      }
    }, 1000, 122);

    //获取验证码


    api.appLoginCode(inputContent);
  },


})